﻿using Corina.Commands;
using Corina.Cydee.DataService;
using Corina.Datamodel.Entities;
using System;
using System.Net;
using System.Windows;
using System.Windows.Input;
using System.Linq;
using Corina.Events;

namespace Corina.Cydee.Apphub.Viewmodels
{
    public class LoginViewmodel : ViewmodelBase
    {
        private WebClient Client;

        public LoginViewmodel()
        {
            Client = new WebClient();
            Client.DownloadStringCompleted += GetPasswordCompleted;
            CoreClient.LoginCompleted += CoreClientLoginCompleted;
        }

        #region Properties
        private string _account = string.Empty;
        public string LoginAccount
        {
            get { return _account; }
            set
            {
                _account = value;
                RaisePropertyChanged(() => LoginAccount);
                RaisePropertyChanged(() => LoginCommand);
            }
        }

        private string _password = string.Empty;
        public string LoginPassword
        {
            get { return _password; }
            set
            {
                _password = value;
                RaisePropertyChanged(() => LoginPassword);
                RaisePropertyChanged(() => LoginCommand);
            }
        }

        private bool _isBusy = false;
        public override bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                RaisePropertyChanged(() => IsBusy);
                RaisePropertyChanged(() => LoginCommand);
            }
        }
        #endregion

        #region Methods
        private void GetPasswordCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            string password = e.Result;
            CoreClient.LoginAsync(LoginAccount, password);
        }

        private void CoreClientLoginCompleted(object sender, LoginCompletedEventArgs e)
        {
            IsBusy = false;
            if (e.Error == null)
            {
                if (e.Result.Result == Enumerations.ResultValue.Succeed)
                {
                    var account = (ManagerInfo)e.Result.Entities.FirstOrDefault();
                    var role = (RoleInfo)e.Result.Entities[1];

                    DataContext.CurrentAccount = account;
                    DataContext.CurrentRole = role;

                    LoginSucceed.Invoke(this, new LoginResultArgs(true));

                }
                else
                {
                    MessageBox.Show(GetOperateResult(e.Result.Result));
                }
            }
            else
            {
                MessageBox.Show(e.Error.Message);
            }
        }

        #endregion

        #region Commands
        public ICommand LoginCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return LoginAccount.Trim().Length > 3 && LoginPassword.Length > 5 && !IsBusy; },
                    ExecuteCallback = delegate
                    {
                        IsBusy = true;
                        Client.DownloadStringAsync(new Uri(WebServiceAddress + "md5.aspx?key=" + LoginPassword, UriKind.RelativeOrAbsolute));
                    }
                };
            }
        }

        public ICommand ExitCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        App.Current.MainWindow.Close();
                    }
                };
            }
        }
        #endregion

        #region Event
        public event EventHandler<LoginResultArgs> LoginSucceed = delegate { };
        #endregion
    }
}
